package Web;

import com.alibaba.fastjson.JSON;
import org.example.PoJo.Message;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/GetMessages")
public class GetMessages  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.setHeader("Refresh", "1000; URL=http://localhose:8080/servlet/welcome.jsp");

        req.setCharacterEncoding("UTF-8");
//        req.getRequestDispatcher("/index.html").forward(req, resp);
//        resp.sendRedirect("/index.html");

//        resp.se
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("sasasasa");
        Map<String,Object>map=new HashMap<>();
        map.put("Messages",req.getSession().getAttribute("Messages"));
        List<Message> lsit=(List<Message>)req.getSession().getAttribute("Messages");
        for (Message message:lsit){
//            System.out.println(message);
//            System.out.println("发送者："+message.getSender());
//            System.out.println("内容："+message.getText());
//            System.out.println("发送时间："+message.getDate());
//            System.out.println("图片文件："+message.getPhoto());
        }
        String json = JSON.toJSONString(map);
        PrintWriter pw = resp.getWriter();
        pw.print(json);
    }
}
